/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxHandlerMethod
 * @Package com.rx.core.handler
 * @Description: 函数处理器
 * @author: 陈锦韬
 * @date: 2021\6\11 0011
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.handler;

import com.rx.core.base.RxBaseData;
import com.rx.core.base.RxBaseException;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.core.util.SpringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: Administrator
 * @Description: 接口处理器
 * @date: 2021\6\11 0011
 */
@Data
@EqualsAndHashCode(callSuper=false)
@Slf4j
public class RxHandlerInterface extends AbstractHandler implements RxHandlerWraper{

    private RxHandlerContent rxHandlerContent;

    RxHandlerInterface(String name, int order){
        init(order,name);
    }

    private String getSimpleName(){
        return RxUtil.StringUtil.firstLower(this.name.substring(name.lastIndexOf(".") + 1));
    }
    @Override
    public boolean handle(RxBaseData data) {
        try {
            if(EmptyChecker.isEmpty(rxHandlerContent)){
                synchronized (this){
                    if(EmptyChecker.isEmpty(rxHandlerContent)){
                        rxHandlerContent = (RxHandlerContent) SpringUtil.getBean(getSimpleName());
                    }
                }
            }
            return rxHandlerContent.handle(data);
        } catch (Throwable e) {
            if (e instanceof RxBaseException){
                RxUtil.CheckUtil.throwOut(RESULT_CODE.MESSAGE,"处理器失败:"+((RxBaseException)e).getResultMessage());
            }else{
                RxUtil.CheckUtil.throwOut(RESULT_CODE.MESSAGE,"处理器失败:"+e.getMessage());
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object object){
        RxHandlerInterface rxHandlerMethod = (RxHandlerInterface)object;
        if (this.rxHandlerContent.equals(rxHandlerMethod.getRxHandlerContent())){
            return true;
        }
        return false;
    }
    @Override
    public int hashCode() {
        return this.rxHandlerContent.hashCode();
    }
}
